package com.etennis.utils;

public class RecordScoreUtils {
    // 单打比赛参数
    private final static Double singleRecordScore=20.0;
    private final static Double singleWinnerBonus=5.0;
    private final static Double singleChallengerBonusRate=0.1;
    private final static Long groupGap=5L;

    // 双打比赛参数
    private final static Double DoubleRecordScore=40.0;
    private final static Double DoubleWinnerBonus=10.0;
    private final static Double DoubleChallengerBonusRate=0.1;
    // private final static Long doubleGroupGap=10L;

    // 单打比赛积分结果
    public static ResultPoint getSingleResult(Long groupIdA, Long groupIdB, int pointA, int pointB){
        Double a=0.0,b=0.0;
        int aScore,bScore;
        // 基于赛局质量的积分分配
        a=singleRecordScore*pointA/(pointA+pointB);
        b=singleRecordScore*pointB/(pointB+pointA);

        // winner奖励分
        if(pointA>pointB){
            a+=singleWinnerBonus;
        } else if (pointB>pointA) {
            b+=singleWinnerBonus;
        }

        // challenger奖励分
        if(groupIdA<groupIdB){
            b+=b*singleChallengerBonusRate*(groupIdB-groupIdA);
        } else if (groupIdB<groupIdA) {
            a+=a*singleChallengerBonusRate*(groupIdA-groupIdB);
        }

        aScore=(int)Math.round(a);
        bScore=(int)Math.round(b);

        return new ResultPoint(aScore,0,bScore,0);
    }

    // 双打比赛积分结果
    public static ResultPoint getDoubleResult(Long groupIdA1, Long groupIdA2, Long groupIdB1,Long groupIdB2, int pointA, int pointB){
        Double a1=0.0,a2=0.0,b1=0.0,b2=0.0;
        int a1Score,a2Score,b1Score,b2Score;

        // 基于赛局质量的组合积分分配
        Double scoreA=DoubleRecordScore*pointA/(pointA+pointB);
        Double scoreB=DoubleRecordScore*pointB/(pointA+pointB);

        // 双打组合水平评定
        Long levelA=groupGap*2-groupIdA1-groupIdA2;
        Long levelB=groupGap*2-groupIdB1-groupIdB2;

        // winner组合奖励分
        if(pointA>pointB){
            scoreA+=DoubleWinnerBonus;
        }else if (pointA<pointB){
            scoreB+=DoubleWinnerBonus;
        }

        // challenger组合奖励分
        if(levelA>levelB){
            scoreB+=scoreB*DoubleChallengerBonusRate*(levelA-levelB);
        }else if(levelA<levelB){
            scoreA+=scoreA*DoubleChallengerBonusRate*(levelB-levelA);
        }

        // 组内贡献度分配
        a1=scoreA*(groupGap-groupIdA1)/levelA;
        a2=scoreA*(groupGap-groupIdA2)/levelA;
        b1=scoreB*(groupGap-groupIdB1)/levelB;
        b2=scoreB*(groupGap-groupIdB2)/levelB;

        a1Score=(int) Math.round(a1);
        a2Score=(int) Math.round(a2);
        b1Score=(int) Math.round(b1);
        b2Score=(int) Math.round(b2);

        return new ResultPoint(a1Score,a2Score,b1Score,b2Score);
    }
}
